package cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.assign.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.domain.task.type.AssignTaskRequest;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import org.junit.Assert;

import java.util.Arrays;

public class AssignAnalyzeWorkReportFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        String firstAuditUserId = startWorkFlowRequest.contextData.getString("firstAuditUserId");
        String secondAuditUserId = startWorkFlowRequest.contextData.getString("secondAuditUserId");

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowTaskQuery.instanceStatusList.add(WorkFlowInstanceStatus.Running.status);

        //一级审批人转交任务给王五
        {
            workFlowTaskQuery.assignerList.add(firstAuditUserId);
            AssignTaskRequest assignTaskRequest = new AssignTaskRequest();
            {
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                assignTaskRequest.taskId = workFlowTaskResponse.taskId;
                assignTaskRequest.assigner = "王五";
                quickWorkFlow.assignWorkFlowTask(assignTaskRequest);
            }
            //查询王五的任务
            {
                workFlowTaskQuery.assignerList = Arrays.asList(assignTaskRequest.assigner);
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                boolean instanceFinished = quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
                Assert.assertFalse("实例当前已经结束", instanceFinished);
            }
        }
        //二级审批人同意
        {
            workFlowTaskQuery.assignerList = Arrays.asList(secondAuditUserId);
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
            boolean instanceFinished = quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            Assert.assertTrue("实例当前已经结束", instanceFinished);
        }
    }

    @Override
    public String name() {
        return "转交分析工作报告任务";
    }

}
